package com.yz.smartpos.yztester.util;

import android.os.Bundle;
import android.util.Log;

import androidx.test.internal.runner.TestExecutor;
import androidx.test.internal.runner.TestRequestBuilder;
import androidx.test.runner.AndroidJUnitRunner;

import org.junit.runner.notification.RunListener;


public class JUnitCaseRunner {
    private RunListener listener ;
    private String pathToScan ;
    private String testClassName;
    private String testMethodName;
    public JUnitCaseRunner(String pathToScan, String testClassName, String testMethodName, RunListener listener){
        this.pathToScan =pathToScan;
        this.testClassName =testClassName;
        this.testMethodName = testMethodName;
        this.listener = listener;
    }

    public Bundle execute(){
        if (null == pathToScan || null ==testClassName || null == testMethodName){
            return null;
        }
        AndroidJUnitRunner androidJUnitRunner = new AndroidJUnitRunner();
        Bundle b= new Bundle();
//        Log.i("11111111111", "start........." + testClassName + ":" + testMethodName);
        TestExecutor.Builder executorBuilder = new TestExecutor.Builder(androidJUnitRunner);
        TestRequestBuilder requestBuilder =new TestRequestBuilder(androidJUnitRunner, b);
        requestBuilder.addTestMethod(testClassName,testMethodName);
        requestBuilder.addPathToScan(pathToScan);
        executorBuilder.addRunListener(listener);
        Bundle results = executorBuilder.build().execute(requestBuilder.build());
        return results;
    }
}
